﻿using blackhouse.NemoBlog.Business;
using blackhouse.NemoBlog.Business.Presenter;
using blackhouse.NemoBlog.Business.View;
using mojoPortal.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.NemoBlog.Web {
    public partial class NemoBlogModule : SiteModuleControl {

        #region Fields

        private PostListPresenter presenter;
        private IPostListView view;
        private NemoBlogSettings settings;

        #endregion

        #region Event Handlers

        //protected override void OnInit(EventArgs e) {
        //    this.EnsureChildControls();
        //    base.OnInit(e);
        //}

        protected override void CreateChildControls() {
            base.CreateChildControls();

            this.settings = new NemoBlogSettings(this.ModuleId);
            IBlogViewFactory viewFactory = settings.Configuration.GetViewFactory();
            this.view = null;
            if (viewFactory != null)
                this.view = viewFactory.LoadPostListView();
            Control viewControl = view as Control;
            if (this.view == null || viewControl == null) {
                viewControl = this.LoadControl("~/StandardNemoBlogModule.ascx");
                this.view = viewControl as IPostListView;
            }
            viewControl.Init += viewControl_Init;
            viewControl.Load += viewControl_Load;

            INeedParentSiteModuleControl supportParentControl = viewControl as INeedParentSiteModuleControl;
            if (supportParentControl != null)
                supportParentControl.ParentControl = this;
            this.pnlModuleWrapper.Controls.Add(viewControl);
        }

        #endregion

        #region Methods

        void viewControl_Init(object sender, EventArgs e) {
            this.presenter = new PostListPresenter(this.view, this.settings.Configuration.GetPostListModel());
        }

        void viewControl_Load(object sender, EventArgs e) {
            this.presenter.Load();
            this.SetParentCssClass();
        }

        public void SetParentCssClass() {
            List<string> cssClasses = new List<string>();
            cssClasses.Add("nemoblog");
            if (this.view.IsPublisher)
                cssClasses.Add("nemoblogadmin");
            this.pnlModuleWrapper.CssClass = string.Join(" ", cssClasses.ToArray());
        }

        #endregion

    }
}